//
//  NYUserSetPwdCell.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2017/12/25.
//  Copyright © 2017年 niuwaketang. All rights reserved.
//

#import "NYUserSetPwdCell.h"
@interface NYUserSetPwdCell ()
@property(nonatomic, strong) QMUIButton *leftView;
@property(nonatomic, strong) UIView *lineView;
@property(nonatomic, strong) UIView *contentCoverView;
@end
@implementation NYUserSetPwdCell
+ (CGFloat)heightForField:(FXFormField *)field width:(CGFloat)width
{
        return __WGWidth(50);
}

- (BOOL)autoAction
{
        return NO;
}
- (void)setUp
{
        [super setUp];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupView];
        self.textField.font = WGSystem16Font;
        self.textField.placeholderFont = WGSystem16Font;
        self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:self.textField];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFielDidEndEditing:) name:UITextFieldTextDidEndEditingNotification object:self.textField];
}

- (void)textFielDidEndEditing:(NSNotification *)noti
{
        self.lineView.backgroundColor = UIColorHex(0xdce1eb);
        self.leftView.selected = NO;
}
- (void)textFieldDidBeginEditing:(NSNotification *)noti
{
        self.lineView.backgroundColor = UIColorHex(0xdce1eb);
        self.leftView.selected = YES;
}
- (void)textFieldValid:(BOOL)valid
{
        if (valid)
        {
                self.field.value = self.textField.validatableText;
        }
}

- (void)setLeftViewImg:(UIImage *)leftViewImg
{
        [self.leftView setImage:leftViewImg forState:(UIControlStateNormal)];
}
- (void)setPlaceHolder:(NSString *)placeHolder
{
        self.textField.placeholder = placeHolder;
}
- (void)setKeyboardType:(UIKeyboardType)keyboardType
{
        self.textField.keyboardType = keyboardType;
}
- (void)setupView
{
        self.contentCoverView = [[UIView alloc] init];
        [self.contentView addSubview:self.contentCoverView];
        [self.contentCoverView addSubview:self.leftView];
        
        self.lineView = [[UIView alloc] init];
        self.lineView.backgroundColor = UIColorHex(0xdce1eb);
        self.textField.placeholderColor = UIColorHex(0xCAD1E0);
        self.textField.placeholderFont = WGSystem14Font;
        self.textField.textColor = UIColorHex(0x3b3b3b);
        self.textField.font = WGSystem16Font;
        [self.textField removeFromSuperview];
        [self.contentCoverView addSubview:self.textField];
        [self.contentCoverView addSubview:self.lineView];
}
- (void)update
{
        [super update];
        self.textLabel.text = nil;
        [self.leftView setTitle:self.field.title forState:(UIControlStateNormal)];
        self.textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        self.textField.textAlignment = NSTextAlignmentLeft;
        [self layout];
}
- (void)layout
{
        [self.contentCoverView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self.contentView);
        }];
        [self.leftView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentCoverView).offset(__WGWidth(16));
                make.centerY.equalTo(self.contentCoverView);
        }];
        
        [self.textField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.leftView.mas_right).offset(__WGWidth(15));
                make.right.equalTo(self.contentCoverView.mas_right).offset(-__WGWidth(15));
                make.centerY.equalTo(self.leftView);
                make.top.greaterThanOrEqualTo(self.contentCoverView).offset(__WGWidth(0));
                make.bottom.lessThanOrEqualTo(self.contentCoverView).offset(-__WGWidth(0));
        }];
        [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentCoverView.mas_left);
                make.right.equalTo(self.contentCoverView.mas_right);
                make.bottom.equalTo(self.contentCoverView);
                make.height.mas_equalTo(__WGWidth(1));
        }];
}
- (QMUIButton *)leftView
{
        if (_leftView == nil) {
                _leftView = [[QMUIButton alloc] initWithFrame:CGRectZero];
                _leftView.contentMode = UIViewContentModeLeft;
                [_leftView setTitleColor:UIColorHex(0x333333) forState:(UIControlStateNormal)];
                _leftView.titleLabel.font = WGSystem15Font;
                _leftView.userInteractionEnabled = NO;
                [_leftView setContentHuggingPriority:(UILayoutPriorityDefaultHigh) forAxis:(UILayoutConstraintAxisHorizontal)];
        }
        return _leftView;
}
- (BOOL)shouldLayoutSubviews
{
        return NO;
}

- (void)dealloc
{
        [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
